package com.example.timetable;
import android.app.Service;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.timetable.method.AlertManage;
import com.example.timetable.method.Database;

@Route(path = "/TimeTable/AlertActivity")
public class AlertActivity extends AppCompatActivity {
    MediaPlayer mediaPlayer;
    Vibrator vibrator;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences preferences=getSharedPreferences("GoToClassAlert",MODE_PRIVATE);
        boolean sound=preferences.getBoolean("sound",false);
        boolean vibrate=preferences.getBoolean("vibrate",false);
        if(vibrate){
            vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
            VibrationEffect vibrationEffect=VibrationEffect.createWaveform(new long[]{500L,500L}, 0);
            vibrator.vibrate(vibrationEffect);
        }
        if(sound){
            mediaPlayer=MediaPlayer.create(this,R.raw.alert);
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
        }
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("上课时间到了")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(sound)
                            mediaPlayer.stop();
                        if(vibrate)
                            vibrator.cancel();
                        finish();
                    }
                }).show();
        AlertManage alertManage=new AlertManage();
        alertManage.initAlert(this,"");
    }
}
